/*
 * Application.java
 *
 * Created on Tre�iadienis, 2006, Rugs�jo 27, 19.35
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package keitykla;

import java.io.*;
import j2mefs.*;

public class Application
{
    private static FileSystem fso;
    private static FileSystem fsoi;
    private static Directory dir;
    private static File file;
    private static String system_name = "keitykl_54";
    //Inicializuojam automatiskai pasikrovus sitai klasei ir pamirstam.
    //Visai aplikacijai naudojam ta pati fso
    static
    {
	initFileSystem();
        initFirst();
    }
    /** Creates a new instance of Application */
    private Application()
    {
    }
    
//---------------------------------------------------------------------------    
    
    private static void initFileSystem()
    {
        try
        {
            if(FileSystem.exists(system_name))
            {
                fsoi=FileSystem.mount(system_name);      //Mountinam esancia fs
                int ss = fsoi.getSize();
            }
            else
            {
                fsoi=FileSystem.make(system_name);       //kuriam nauja
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

//-------------------currency directory--------------------------------------    
    
    public static Directory getCurrenciesDirectory()
    {
        try
        {
            Directory root = fsoi.getRootDirectory();
            String dir_name = "currencies";
            if(root.directoryExists(dir_name))
                dir=root.getDirectory(dir_name);
            else
                dir=root.makeDirectory(dir_name);
        }    
        catch(Exception e)
        {
            e.printStackTrace();
        }
        
        return dir;
    }
//-----------------------settings directory----------------------------------    

    public static Directory getSettingsDirectory()
    {
        try
        {
            Directory root = fsoi.getRootDirectory();
            String dir_name = "settings";
            if(root.directoryExists(dir_name))
                dir=root.getDirectory(dir_name);
            else
                dir=root.makeDirectory(dir_name);
        }    
        catch(Exception e)
        {
            e.printStackTrace();
        }
        
        return dir;
    }
   
//---------------------------------------------------------------------------    
    
    public static File createOrLoadFile(Directory dir, String file_name)
    {
        try
        {
            if(dir.fileExists(file_name))
                file=dir.getFile(file_name);
            else
                file=dir.makeFile(file_name);
        }
        catch(Exception e)
        {
            e.printStackTrace();            
        }
        
        return file;
    }
    
//---------------------------------------------------------------------------    
    
    public static void deleteFile(Directory directory, String file_name)
    {
        Directory dir = directory;

        try
        {
            dir.deleteFile(file_name);
        }
        catch(FileSystemException e)
        {
            e.printStackTrace();            
        }
    } 
    
//---------------------------------------------------------------------------    
    
    public static void writeFile(File file, String stream)
    {
        try
        {
            file.write(stream);
        }
        catch(Exception e)
        {
            e.printStackTrace();            
        }
    } 

//---------------------------------------------------------------------------    
    
    public static String readFile(String file_name, String directory_name)
    {
        String data = "";
        try
        {
            file = fsoi.getFile("/"+directory_name+"/"+file_name);
            data = file.readString();
        }
        catch(Exception e)
        {
            e.printStackTrace();            
        }
        return data;
    }  
    
//---------------------------------------------------------------------------    
    
    public static void appendFile(File file, String stream)
    {
        try
        {
            file.append(stream);
        }
        catch(Exception e)
        {
            e.printStackTrace();            
        }
    }     
    
//-----------------------------------------------------------------------------    
    
    public static void initFirst()
    {
        Directory dir = Application.getCurrenciesDirectory();
        int files = dir.getFileCount();
        if(files<1)
        {
            Storage.save();
            String Names = "http://webservices.lb.lt/ExchangeRates/ExchangeRates.asmx/getListOfCurrencies";
            String Rate = "http://webservices.lb.lt/ExchangeRates/ExchangeRates.asmx/getCurrentExchangeRate?Currency=";
            String Rates = "http://webservices.lb.lt/ExchangeRates/ExchangeRates.asmx/getExchangeRatesByDate?Date=";
            ByteArrayOutputStream data;
            try
            {
                Application.writeFile(Application.createOrLoadFile(Application.getSettingsDirectory(), "Names"), Names);
                Application.writeFile(Application.createOrLoadFile(Application.getSettingsDirectory(), "Rate"), Rate);
                Application.writeFile(Application.createOrLoadFile(Application.getSettingsDirectory(), "Rates"), Rates);
                Settings.setToCurrency("LTL");
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
        }
        else
            Storage.getCurrency();

    }    
    
}
